package service

import (
	"strconv"
	"time"
	"zimuzu_cvn_web_api/config"
	"zimuzu_cvn_web_api/model"
	"zimuzu_cvn_web_api/pkg/drive/redis"
	. "zimuzu_cvn_web_api/pkg/e"
)

//全网供挖出的矿数量
const NetMineAmount = 273973

type RouterUploadInfoStr struct {
	UpdateTime      string  `json:"updateTime"`
	TotalContribute int64   `json:"totalContribute"`
	MineAmount      float64 `json:"mineAmount"`
}

//GetNetTotalUpload 获取全网上传量数据
//Auth: Xu
func GetNetTotalUpload() (totalUploadInfo RouterUploadInfoStr, err error) {
	netTotalUpload, err := model.GetTodayRouterMining(config.C.DB.DB, model.GlobalNameNetTotalUpload)
	if err != nil {
		Log("err", "查询全网总流量失败！", err.Error())
	}
	netTatalUploadSize, _ := strconv.ParseInt(netTotalUpload.Value, 10, 64)
	var updateTime string
	if netTotalUpload.UpdateTime != 0 {
		t := time.Unix(netTotalUpload.UpdateTime, 0)
		updateTime = strconv.Itoa(t.Hour()) + ":" + strconv.Itoa(t.Minute())
	} else {
		updateTime = ""
	}

	totalUploadInfo = RouterUploadInfoStr{updateTime, netTatalUploadSize, NetMineAmount}

	return
}

type NetMillInfo struct {
	TotalNodeCount int   `json:"totalNodeCount"  mapstructure:"totalNodeCount"`
	TotalUpload    int64 `json:"totalUpload" mapstructure:"totalUpload"`
	SaleAmount     int   `json:"saleAmount" mapstructure:"saleAmount"`
	MiningAmount   int   `json:"miningAmount" mapstructure:"miningAmount"`
}

//获取全网矿机信息
func GetNetMillInfo() NetMillInfo {
	var netMillInfo NetMillInfo
	millInfoCacheKey := PublicCacheKeyPre + MillInfoHashCacheKey
	if redis.Exists(millInfoCacheKey) {
		userInfoMap, err := redis.HGetAll(millInfoCacheKey)
		if err != nil {
			Log("err", "获取全网矿机统计信息缓存失败", err.Error())
		}
		Map2Struct(netMillInfo, &netMillInfo, userInfoMap)
		//mapstructure.Decode(userInfoMap,&netMillInfo)
	} else {
		//linux数
		totalNodeCountAdd, _ := model.GetGlobalVar(model.GlobalNameLxNodesAddNum)
		totalNodeCountAddInt, _ := strconv.Atoi(totalNodeCountAdd)
		totalNodeCount, _ := model.GetLinuxNodeCount(config.C.DB.DB)
		netMillInfo.TotalNodeCount = totalNodeCount + totalNodeCountAddInt
		//linux节点总上传量
		totalUploadAdd, _ := model.GetGlobalVar(model.GlobalNameLxFlowAddNum)
		totalUploadAddInt, _ := strconv.ParseInt(totalUploadAdd, 10, 64)
		totalUpload, _ := model.GetLinuxFlowSum(config.C.DB.DB)
		netMillInfo.TotalUpload = totalUpload*1024 + totalUploadAddInt*1024*1024*1024*1024
		//路由器已售数量
		routerCounts, _ := model.GetGlobalVar(model.GlobalNameNetRouterCounts)
		netMillInfo.SaleAmount, _ = strconv.Atoi(routerCounts)

		userInfoMap := Struct2Map(netMillInfo)

		_, err := redis.HSet(millInfoCacheKey, userInfoMap, 3600)
		if err != nil {
			Log("err", "设置全网矿机统计信息缓存失败")
		}
	}
	return netMillInfo
}

//获取全网最近7日平均收益
//Auth:Xu
func GetNetAverageIncome(glabalName string) float64 {
	cacheKey := "cvn:public" + glabalName
	var average float64
	if redis.Exists(cacheKey) {
		averageStr, _ := redis.Get(cacheKey)
		average, _ = strconv.ParseFloat(averageStr, 64)
	} else {
		averageF, err := model.GetGlobalVar(glabalName)
		if err != nil {
			Log("err", "查询全网最近日期平均收益失败")
			average = 0
		}
		redis.Set(cacheKey, averageF, 3600)
		average, _ = strconv.ParseFloat(averageF, 64)
	}
	return average
}
